/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.action_user_preferences', {
		
		_init: function() {

			var o = this.element;
			
			o.find( '.Part' ).action_user_preferences_part();
			o.find( '.TabPane' ).widget_tabpane();
			
			o.addClass( 'UserPreferencesWindow' );
			
			$('.Buttons a', this.element).click( function() {
				o.window( 'close' );
			} );
		}
	} );
	
	$.widget( 'ui.action_user_preferences_part', {
		
		_init: function() {
			
			var $this = this;
			var change = $('h4 a', this.element);
			var modify = this.element.find( '.Modify' );
			var description = this.element.find( '.Description' );
			this.message = change.text();

			change.click( function() {
				if( $this.element.hasClass( 'Changing' ) ) {
					$this.element.removeClass( 'Changing' );
					modify.hide();
					description.show();
					$(this).html( $this.message );
				}
				else {
					modify.show();
					description.hide();
					$this.element.addClass( 'Changing' );
					$(this).html( 'hide' );
				}
			} );
		
			this._initForm();
		},
		
		_initForm: function() {
		
			var form = $('form', this.element);
			form.submit( function() {
				var data = {};
				data[ 'id' ] = $(this).closest( '[user_id]' ).attr( 'user_id' );
				$('input[type=text], input[type=password], input[type=hidden]', this).each( function() {
					var o = $(this);
					data[ o.attr( 'name' ) ] = o.attr( 'value' );
				} );
				$.wajax( form.attr( 'action' ), data, function( json ) {
					if( json.status != 'success' ) {
						return false;
					}
					location.reload();
				} );
				return false;
			} );
			
			form.find( 'a' ).click( function() {
				form.submit();
			} );
		}
	} );
	
})(jQuery);
